package com.gao.forkjoin;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.stream.LongStream;

public class Test {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        test3();
    }
//
    public static void test1(){
        long sum = 0L;
        long start = System.currentTimeMillis();


        for (long i = 1L; i < 20_0000_0000; i++) {
            sum += i;
        }

        long end = System.currentTimeMillis();

        System.out.println("sum = " + sum + "时间 ： " + (end - start));
    }
    public static void test2() throws ExecutionException, InterruptedException {
        long start = System.currentTimeMillis();

        ForkJoinPool forkJoinPool = new ForkJoinPool();
        ForkJoinTask<Long> task = new ForkJoinDemo(0L, 20_0000_0000L);
//        提交任务
        ForkJoinTask<Long> submit = forkJoinPool.submit(task);
        Long sum =  submit.get();



        long end = System.currentTimeMillis();
        System.out.println("sum = " + sum + "时间 ： " + (end - start));
    }
    public static void test3(){
        long start = System.currentTimeMillis();
//        stream
        long sum = LongStream.rangeClosed(0L,20_0000_0000L).parallel().reduce(0,(a,b) -> Long.sum(a,b));

        long end = System.currentTimeMillis();
        System.out.println("sum = " + sum + "时间 ： " + (end - start));
    }
}
